home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2593 < prev    next >
Encoding:
Text File  |  1996-08-06  |  4.8 KB  |  109 lines

  1. Path: news.jhu.edu!news
  2. From: Harold Bien <hbien@bme.jhu.edu>
  3. Newsgroups: comp.object,comp.lang.c++,comp.ai.alife,sci.comp-aided
  4. Subject: [Q] Growing Objects
  5. Date: Thu, 18 Jan 1996 12:25:43 -0500
  6. Organization: Johns Hopkins University
  7. Message-ID: <30FE8297.41C6@bme.jhu.edu>
  8. NNTP-Posting-Host: 128.220.160.162
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0b4 (X11; I; IRIX 5.3 IP22)
  13.  
  14. First - thanks to all who responded to my earlier question.  I
  15. found all the comments helpful and was (ashamedly) surprised at the
  16. number of responses.
  17.  
  18.      That said, I've decided to implement a "possible" solution which
  19. has not yet been discussed.  Before I begin, let me preface this with
  20. the fact that I've invested quite a lot of time and effort into the 
  21. simulation already and thus am not very willing to switching languages.  
  22. Therefore, my current implementation is in C++ - my original language of 
  23. choice.  However, I did look into SmallTalk as per one suggestion and 
  24. found that SmallTalk would have been better if it were not for the fact 
  25. that it's an interpreted language (or at least the version we have).  
  26. Anyway - this is all off topic.....
  27.  
  28.      In reality, each cell carries all the genetic codes which it 
  29. requires with itself from the moment of "birth" (mitosis).  Yes, the
  30. cell does control it genetic expression and hence its function, but the
  31. fact remains that ALL THE RAW MATERIALS existed in the cell - it's mere
  32. a function of selecting which ones to "turn on."
  33.      Following this logic then, I decided that I wished to pack all the 
  34. functionality of the immature/growing/mature cell into one large object.  
  35. This gives me the advantage of recycling common code and even sharing 
  36. common data.
  37.      The problem now was selecting which function to run.  Since this is 
  38. not only being posted in comp.lang.c++, I will try and refrain from
  39. making the following description C++ specific, but if I do, please
  40. excuse me - I've had no formal training in OO parlance....
  41.      My solution was to create an object "Cell."  It has the common data 
  42. members of cells in general (age, size, position, etc.), as well as the 
  43. common member functions (Run, Grow, Move, etc.) where "Run" is the 
  44. "function of life."  Because computers are sequencial, I cannot run all 
  45. my cells simultaneously.  Therefore, I have to implement the function 
  46. "Run" which will run my cells sequencially.
  47.      This "Run" function will call another function indirectly.  In 
  48. simpler terms, it "opens up a box labelled RunMe" and whatever was in
  49. the box then gains control, and here's where the exciting part comes.
  50. The function which is "in the box" _CHANGES_OVER_TIME.  Therefore, the 
  51. functionality of the object "Cell" changes over time.
  52.      Since this is all very abstract, I'm going to try to explain it 
  53. using C++ (which I hope most of you are familiar with (I know, this is 
  54. not comp.lang.c++, but very often C is chosen as an example language)...
  55.      The class Cell has a function Run() which takes no arguments but 
  56. calls another function through a pointer to a member function.  Then, 
  57. when that member function runs, it updates a variable 'age' and checks
  58. to see if it is mature.  If so, then it will change the pointer to point
  59. to another function.  Here's what it will look like:
  60.  
  61. class Cell
  62. {
  63.  public: // These fuctions are accessible to the public
  64.       Cell(); /* This is the constructor - it gets called first when the 
  65.                  object is created */
  66.  // Run is the function which gets called from the 'outside'
  67.  void Run();
  68.  // PreMature is the function which runs when Run() is called and age<4
  69.  void Run_PreMature();
  70.  // PostMature is the function which is called when Run() is invoked 
  71.  //   and age >=4
  72.  void Run_PostMature();
  73.  
  74.  private:
  75.         void    (*Cell::RunFunc)(); /* Pointer to a cell function */
  76.         unsigned int    age;
  77. };
  78.         
  79.         Cell::Cell() /* Constructor */
  80. {
  81.  age=0; // Newborn
  82.  RunFunc=Run_PreMature; /* Point RunFunc to PreMature() function */
  83. }
  84.  
  85. void    Cell:Run_PreMature()
  86. {
  87.  age=age+1; /* I know: (age++), but for those who don't know C... */
  88.  if (age>=4) RunFunc=Run_PostMature; /* If age greater than or equal to
  89. 4
  90.                                         then point RunFunc to
  91. PostMature()
  92.                                      */
  93. }
  94.  
  95.  
  96.         This approach reflects real cells in that all the code which it 
  97. needs for functioning are contained within.  It is only a matter of the 
  98. selection of which code segment (or genetic segment) to use (or
  99. replicate).
  100.  
  101.      I'd certainly appreciate criticisms on this approach before I dig 
  102. in.  If anyone has had experience in doing the above or another similar 
  103. method is superior to the above, I'd love to hear about it.
  104.  
  105.         Again, thanks to all who replied...
  106.  
  107.  
  108.                                                 Harold
  109.